#!/usr/bin/python2.5 


import sys
sys.path.append("../../")


import time
from runtime import *
from game import *
from pygame.locals import *



def create_pickled_game():

    """
    ############################################################################
    START OF GAME CODE
    """

    # Create a new Game
    mytestGame = Game("Gamebaker - pong")
    # Create a Game-Screen / Level
    mytestgamescreen = mytestGame.gamescreen((800,600),"Pongscreen")
    mytestGame.startscreen = "Pongscreen"


    # Create GameObjects to show in the game - will later need a sprite attatched
    ball = mytestGame.gameobject("ball")
    bat = mytestGame.gameobject("bat")
    bat2 = mytestGame.gameobject("bat2")
    score = mytestGame.gameobject("score")

    # Add the Ball Sprite
    ballsprite = mytestGame.sprite("ballspr")
    ballsprite.imagefiles.append({"filename":"sprites/images/ball.bmp", "topleft":(0,0), "bottomright":(25,25)})
    ballsprite.framerate = 20
    ballsprite.alphakey = (0,255,0)
    #tell the main gameobject to use this
    ball.sprite = "ballspr"

    paddlesprite = mytestGame.sprite("paddlespr")
    paddlesprite.imagefiles.append({"filename":"sprites/images/paddle.bmp", "topleft":(0,0), "bottomright":(15,75)})
    paddlesprite.framerate = 20
    paddlesprite.alphakey = (0,255,0)
    #tell the main gameobjects to use this

    bat.sprite = "paddlespr"
    bat2.sprite = "paddlespr"





    # Say that we want this object to appear immediately in our game-screen
    mytestgamescreen.startobjects = [ball,bat,bat2,score]


    # Ball
    ballws = mytestGame.workstate("WSball")
    ballws.actions[EVENT_INIT] = """
self.x=400
self.y=300
self.vx=10
self.vy=0
self.resize(25,25)"""

    ballws.actions[EVENT_ITERATION] = """
self.move_velocity()

if gamescreen.check_collisions("ball","bat"):
    self.vy += (gamescreen.get_object('ball').y-(gamescreen.get_object('bat').y))/7
    #The height of a paddle is 75 pixels, the ball deflects from the 'center'
    self.vx = -self.vx
    # Do this or the sprites will overlap
    self.move_velocity()

else:
    if gamescreen.check_collisions("ball","bat2"):
        self.vy += (gamescreen.get_object('ball').y-(gamescreen.get_object('bat2').y))/7
        self.vx = -self.vx
        # Do this or the sprites will overlap
        self.move_velocity()
"""


    ballws.actions[EVENT_OFFSCREEN] = """
if (self.y < 0) or (self.y > 600):
	self.vy = -self.vy

if (self.x < 0) or (self.x > 800):
    if self.x < 0:
        game.score -=1
    elif self.x > 800:
        game.score +=1
    self.x=400
    self.y=300
    self.vy=0
"""

    ball.baseworkstate = "WSball"




    # Score Object
    scorews = mytestGame.workstate("WSscore")

    scorews.actions[EVENT_INIT] = """
self.x = 0
self.y = 0
self.color=(255,255,255)
self.resize(70,25)
"""
    
    scorews.actions[EVENT_ITERATION] = """
self.clear()
self.draw_text(game.score,28,(0,0,255))

"""

    score.baseworkstate = "WSscore"

    # Player Paddle
    paddlews = mytestGame.workstate("WSpaddle1")

    paddlews.actions[EVENT_INIT] = """
self.x = 10
self.y = 300
self.vx = 0
self.vy = 0
self.resize(15,75)
"""

    paddlews.actions[EVENT_ITERATION] = """
self.move_velocity()
"""

    paddlews.actions[EVENT_KEYDOWN*K_DOWN] = """
self.vy = 12
"""

    paddlews.actions[EVENT_KEYDOWN*K_UP] = """
self.vy = -12
"""

    paddlews.actions[EVENT_KEYUP*K_UP] = """
self.vy = 0
"""
    paddlews.actions[EVENT_KEYUP*K_DOWN] = paddlews.actions[EVENT_KEYUP*K_UP]

    bat.baseworkstate = "WSpaddle1"


    # Enemy Paddle
    paddlews2 = mytestGame.workstate("WSpaddle2")
    paddlews2.actions[EVENT_INIT] = """
self.x = 765
self.y = 300
self.vx= 0
self.vy = 0
self.resize(15,75)
"""
    paddlews2.actions[EVENT_ITERATION] = """
from random import randint
by = gamescreen.get_object('ball').y

if game.score < -5:
	score = -5
else:
	score = game.score
#The enemy paddle might go the wrong way with a low score
if self.y+34 < by-5:
	self.vy = 8 + randint(-3,3) + score
elif self.y+34 > by+5:
	self.vy = -8 + randint(-3,3) - score
else:
	self.vy = 0
self.move_velocity()

"""
    bat2.baseworkstate = "WSpaddle2"
    


    """
    *********************************************************************************
    END OF GAME CODE
    """





    import gbfileio
    gbfileio.savegame("testgame.gb",mytestGame,method="pickle")


    game = gbfileio.loadgame("testgame.gb")
    run_game(game)



if __name__ == "__main__":
    create_pickled_game()

